package org.yuedu;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/memory")
public class MemoryResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMemoryInfo() {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();  // 已分配内存
        long freeMemory = runtime.freeMemory();    // 空闲内存
        long usedMemory = totalMemory - freeMemory;
        long maxMemory = runtime.maxMemory();      // 最大可分配内存

        return String.format(
                """
                        Memory Usage:
                        Used:  %,d MB
                        Free:  %,d MB
                        Total: %,d MB
                        Max:   %,d MB""",
                usedMemory / 1024 / 1024,
                freeMemory / 1024 / 1024,
                totalMemory / 1024 / 1024,
                maxMemory / 1024 / 1024
        );
    }
}